package com.hailiao.bd.demo;

import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.clj.fastble.data.BleDevice;
import com.hailiao.bd.demo.app.MyApp;
import com.hailiao.bdproto2p1.bean.basic.BDICI_Bean;
import com.hailiao.bdproto2p1.bean.basic.CCICA_Bean;
import com.hailiao.bdproto2p1.worker.BasicWorker2p1;

import java.util.List;

import kotlin.coroutines.Continuation;
import kotlin.coroutines.CoroutineContext;
import kotlinx.coroutines.Dispatchers;

public class MainActivity extends AppCompatActivity {

    private TextView txtTest;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txtTest = findViewById(R.id.txt_test);

        txtTest.setOnClickListener(view -> {
            BasicWorker2p1.INSTANCE.ccica(MyApp.instant.ble, "", new CCICA_Bean(), new Continuation<BDICI_Bean>() {
                @NonNull
                @Override
                public CoroutineContext getContext() {
                    // 在IO线程进行
                    return (CoroutineContext) Dispatchers.getIO();
                }

                @Override
                public void resumeWith(@NonNull Object o) {
                    // 得到结果
                    BDICI_Bean bean = (BDICI_Bean) o;
                }
            });
        });

    }

    @Override
    protected void onStart() {
        super.onStart();
    }
    
    // 扫描并连接
    private void scan2connect() {
        MyApp.instant.ble.scanDevice(new Continuation<List<? extends BleDevice>>() {
            @NonNull
            @Override
            public CoroutineContext getContext() {
                // 在IO线程进行
                return (CoroutineContext) Dispatchers.getIO();
            }

            @Override
            public void resumeWith(@NonNull Object o) {
                // 得到结果
                List<BleDevice> list = (List<BleDevice>) o;
                BleDevice device;
                for (BleDevice item: list) {
                    if (item.getName().toUpperCase().contains("IRIM")) {
                        device = item;
                    }
                }
            }
        });
    }
}